import os
import re
import time
import random
import logging
import requests
import datetime
import traceback
from abc import ABC, abstractmethod
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from config.settings import EDGE_DRIVER_PATH, HEADERS, logger


class BaseSpider(ABC):
    """
    爬虫基类，提供通用功能
    """
    
    def __init__(self):
        self.logger = logger
        self.headers = HEADERS
        
    def _get_driver(self, headless=True):
        """
        获取浏览器驱动
        """
        opt = Options()
        if headless:
            opt.add_argument('--headless')
        opt.add_argument('--disable-gpu')
        opt.add_argument('--start-maximized')
        try:
            return webdriver.Edge(service=Service(EDGE_DRIVER_PATH), options=opt)
        except Exception as e:
            self.logger.error(f"无法启动Edge浏览器: {str(e)}")
            self.logger.error("请确保已下载与您Edge浏览器版本匹配的WebDriver并放在项目根目录下")
            raise
            
    def _fetch(self, url, encoding=None):
        """
        获取网页内容
        """
        try:
            resp = requests.get(url, headers=self.headers, timeout=15)
            resp.encoding = encoding or resp.apparent_encoding
            return resp.text
        except Exception as e:
            self.logger.error(f"获取网页内容失败 {url}: {str(e)}")
            return ""
            
    @abstractmethod
    def crawl(self):
        """
        抽象方法，子类必须实现
        """
        pass